﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YiSha.Business.YiDongYuan;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Web.Code;

namespace YiSha.Admin.WebApi.Controllers
{
    /// <summary>
    /// 尾气检测接口
    /// </summary>
    [Route("[controller]/[action]")]
    [ApiController]
    [AuthorizeFilter]
    public class DetectionController : ControllerBase
    {
        private DetectionTaskBLL taskBLL = new DetectionTaskBLL();
        private DetectionMechanicsBLL dmBLL = new DetectionMechanicsBLL();
        private DetectionResultBLL resultBLL = new DetectionResultBLL();
        private DetectionInfoBLL infoBLL = new DetectionInfoBLL();

        #region 获取数据

        /// <summary>
        /// 根据用户Id获取任务列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<List<DetectionTaskEntity>>> GetListByUser([FromQuery] long userId)
        {
            TData<List<DetectionTaskEntity>> obj = await taskBLL.GetListByUserId(userId);
            return obj;
        }

        /// <summary>
        /// 获取任务信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<DetectionTaskEntity>> GetForm([FromQuery] long id)
        {
            TData<DetectionTaskEntity> obj = await taskBLL.GetEntity(id);
            return obj;
        }

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<DetectionMechanicsEntity>> GetMechanicsForm([FromQuery] long id)
        {
            TData<DetectionMechanicsEntity> obj = await dmBLL.GetEntity(id);
            return obj;
        }

        /// <summary>
        /// 获取烟度计检测结果
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<DetectionResultEntity>> GetResultInfo([FromQuery] long userId, [FromQuery] string code)
        {
            TData<DetectionResultEntity> obj = await resultBLL.GetLastResult(userId, code);
            return obj;
        }

        #endregion 获取数据

        #region 提交数据

        /// <summary>
        /// 保存机械信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<TData<string>> SaveMechanics([FromForm] DetectionMechanicsEntity entity)
        {
            if (string.IsNullOrEmpty(entity.OwnerName))
                throw new Exception("请输入所有人");
            if (string.IsNullOrEmpty(entity.OwnerTel))
                throw new Exception("请输入联系方式");
            if (string.IsNullOrEmpty(entity.OwnerCard_Pic))
                throw new Exception("请上传所有人证件照片");
            if (string.IsNullOrEmpty(entity.JXDJLX))
                throw new Exception("请选择机械登记类型");
            if (string.IsNullOrEmpty(entity.JXLB))
                throw new Exception("请输入机械类别");
            if (string.IsNullOrEmpty(entity.RLLX))
                throw new Exception("请选择燃料种类");
            if (string.IsNullOrEmpty(entity.PFJD))
                throw new Exception("请选择排放阶段");
            if (string.IsNullOrEmpty(entity.JX1_pic))
                throw new Exception("请上传机械正面照片");
            if (string.IsNullOrEmpty(entity.JX2_pic))
                throw new Exception("请上传机械侧面照片");
            if (string.IsNullOrEmpty(entity.JX3_pic))
                throw new Exception("请上传机械尾部照片");
            if (string.IsNullOrEmpty(entity.FDJMP_pic))
                throw new Exception("请上传发动机铭牌");
            if (string.IsNullOrEmpty(entity.FDJGL))
                throw new Exception("请输入发动机额定功率");
            if (entity.FDJQGS.IsNullOrEmpty() || entity.FDJQGS == 0)
                throw new Exception("请输入发动机气缸数");
            if (!entity.HBXXGKBH.IsNullOrEmpty() && entity.HBXXGKBH.Length != 24)
                throw new Exception("信息公开编号长度为24位（不含空格）");
            if (!entity.FDJXSHZZH.IsNullOrEmpty() && entity.FDJXSHZZH.Length != 16)
                throw new Exception("发动机型式核准号应为16位（不含空格）");
            TData<string> obj = await dmBLL.SaveForm(entity);
            return obj;
        }

        /// <summary>
        /// 保存检测信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<TData<string>> SaveDetectionInfo([FromForm] DetectionInfoEntity entity)
        {
            string token = Request.Headers["ApiToken"].ParseToString();
            OperatorInfo user = await Operator.Instance.Current(token);
            if (entity.Address.IsNullOrEmpty())
                throw new Exception("请输入检测地址");
            if (entity.VideoId.IsNullOrEmpty())
                throw new Exception("请上传检测视频");
            if (entity.ResultDataId.IsNullOrZero())
                throw new Exception("请先获取检测数据");
            TData<string> obj = await infoBLL.SaveForm(entity, user);
            return obj;
        }

        /// <summary>
        /// 更爱环保号牌
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newCode"></param>
        /// <returns></returns>
        public async Task<TData<string>> SaveChangeCode([FromForm] long id, [FromForm] string newCode)
        {
            TData<string> obj = await taskBLL.SaveChangeCode(id, newCode);
            return obj;
        }

        #endregion 提交数据
    }
}